package com.yisu.expressway.api.domain.config;

import com.alibaba.fastjson.JSON;
import io.github.xdiamond.client.annotation.OneKeyListener;
import io.github.xdiamond.client.event.ConfigEvent;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * Created by Administrator on 2016/8/25.
 */
@Service
public class SystemConfig {
    @Value("${api.default.pageSize}")
    private int defaultPageSize;
    @Value("${api.default.pageIndex}")
    private int defaultPageIndex;
    @Value("${api.order.PaymentDeadlineTime}")
    private int PaymentDeadlineTime;
    @Value("${getUserInfo.api.url}")
    private String getUserInfoApiUrl;
    @Value("${api.notifyPush.url}")
    private String notifyPushUrl;
    @Value("${api.order.cancel.time.limit}")
    private int orderCancelTimeLimit;
    @Value("${api.confirm.order.days}")
    private int confirmOrderDays;


    public int getDefaultPageSize() {
        return defaultPageSize;
    }
    @OneKeyListener(key = "api.default.pageSize")
    public void setDefaultPageSize(ConfigEvent event) {
        try {
            if (event != null) {
                defaultPageSize = Integer.parseInt(String.valueOf(JSON.parseObject(event.toString()).get("value")));
            }
        } catch (Exception ex) {
            defaultPageSize = 0;
        }
    }

    public int getDefaultPageIndex() {
        return defaultPageIndex;
    }
    @OneKeyListener(key = "api.default.pageIndex")
    public void setDefaultPageIndex(ConfigEvent event) {
        try {
            if (event != null) {
                defaultPageIndex = Integer.parseInt(String.valueOf(JSON.parseObject(event.toString()).get("value")));
            }
        } catch (Exception ex) {
            defaultPageIndex = 0;
        }
    }

    public int getPaymentDeadlineTime() {
        return PaymentDeadlineTime;
    }
    @OneKeyListener(key = "api.order.PaymentDeadlineTime")
    public void setPaymentDeadlineTime(ConfigEvent event) {
        try {
            if (event != null) {
                PaymentDeadlineTime = Integer.parseInt(String.valueOf(JSON.parseObject(event.toString()).get("value")));
            }
        } catch (Exception ex) {
            PaymentDeadlineTime = 0;
        }
    }

    public String getGetUserInfoApiUrl() {
        return getUserInfoApiUrl;
    }
    @OneKeyListener(key = "getUserInfo.api.url")
    public void setGetUserInfoApiUrl(ConfigEvent event) {
        try {
            if (event != null) {
                getUserInfoApiUrl = String.valueOf(JSON.parseObject(event.toString()).get("value"));
            }
        } catch (Exception ex) {
            getUserInfoApiUrl = null;
        }
    }

    public String getNotifyPushUrl() {
        return notifyPushUrl;
    }
    @OneKeyListener(key = "getUserInfo.api.url")
    public void setNotifyPushUrl(ConfigEvent event) {
        try {
            if (event != null) {
                notifyPushUrl = String.valueOf(JSON.parseObject(event.toString()).get("value"));
            }
        } catch (Exception ex) {
            notifyPushUrl = null;
        }
    }

    public int getOrderCancelTimeLimit() {
        return orderCancelTimeLimit;
    }

    @OneKeyListener(key = "api.order.cancel.time.limit")
    public void setOrderCancelTimeLimit(ConfigEvent event) {
        try {
            if (event != null) {
                orderCancelTimeLimit = Integer.valueOf(String.valueOf(JSON.parseObject(event.toString()).get("value")));
            }
        } catch (Exception ex) {
            orderCancelTimeLimit = 30;
        }
    }

    public int getConfirmOrderDays() {
        return confirmOrderDays;
    }

    @OneKeyListener(key = "api.confirm.order.days")
    public void setConfirmOrderDays(ConfigEvent event) {
        try {
            if (event != null) {
                confirmOrderDays = Integer.valueOf(String.valueOf(JSON.parseObject(event.toString()).get("value")));
            }
        } catch (Exception ex) {
            confirmOrderDays = 30;
        }
    }
}
